#include"../mqserver/mq_consumer.hpp"
#include<gtest/gtest.h>
mq::ConsumerManager::ptr bmp;

class ConsumerTest : public testing::Environment{
    public:
        virtual void SetUp() override{
            bmp = std::make_shared<mq::ConsumerManager>();
        }
        virtual void TearDown() override{
            bmp->clear();
        }
};
void cb(const std::string& ctag, mq::BasicProperty* bp,const std::string& body){
    std::cout<<ctag<<" 消费了： "<<body<<std::endl;
}

TEST(Consumer_Test,init_test){
    bmp->initQueueConsumer("queue1");
    ASSERT_EQ(bmp->isEmptyQueueConsumer("queue1"),true);
    ASSERT_EQ(bmp->exists("queue1","consumer1"),false);

    bmp->createConsumer("consumer1","queue1",false,cb);
    ASSERT_EQ(bmp->isEmptyQueueConsumer("queue1"),false);
    ASSERT_EQ(bmp->exists("queue1","consumer1"),true);

    bmp->createConsumer("consumer2","queue1",false,cb);
    mq::Consumer::ptr consumer = bmp->choose("queue1");
    ASSERT_NE(consumer.get(),nullptr);
    ASSERT_EQ(consumer->tag,"consumer1");

    mq::Consumer::ptr consumer2 = bmp->choose("queue1");
    ASSERT_NE(consumer2.get(),nullptr);
    ASSERT_EQ(consumer2->tag,"consumer2");

    // bmp->destoryQueueConsumer("queue1");
}

TEST(Consumer_Test,remove_test){
    bmp->removeConsumer("consumer1","queue1");
    ASSERT_EQ(bmp->exists("queue1","consumer1"),false);
    ASSERT_EQ(bmp->exists("queue1","consumer2"),true);

    bmp->removeConsumer("consumer2","queue1");
    ASSERT_EQ(bmp->exists("queue1","consumer1"),false);
    ASSERT_EQ(bmp->exists("queue1","consumer2"),false);

    bmp->destoryQueueConsumer("queue1");
    ASSERT_EQ(bmp->isEmptyQueueConsumer("queue1"),true);
}


int main(int argc,char* argv[]){
    testing::InitGoogleTest(&argc,argv);
    testing::AddGlobalTestEnvironment(new ConsumerTest);
    return RUN_ALL_TESTS();
}